package com.github.thinwonton.cas.showcase.common;


import org.apache.commons.lang3.StringUtils;

public class PasswordUtils {

    private static String defaultAlgorithm = "SHA-1";


    /**
     * 加密密码
     *
     * @param password
     * @param salt
     * @return
     */
    public static String encryptPassword(String password, String salt) {
        return encryptPassword(password, salt, defaultAlgorithm);
    }

    public static String encryptPassword(String password, String salt, String algorithm) {
        if (StringUtils.isBlank(salt)) {
            return null;
        }

        if (StringUtils.isBlank(password)) {
            return null;
        }

        String confusedPassword = confuse(password, salt);
        if (StringUtils.isBlank(algorithm)) {
            algorithm = defaultAlgorithm;
        }
        return DigestUtils.digest(confusedPassword, algorithm);
    }

    /**
     * 混淆密码
     * @param password
     * @param salt
     * @return
     */
    private static String confuse(String password, String salt) {
        return password + salt;
    }
}
